﻿using CodeSystem.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CodeSystem
{
    public partial class TimerTaskForm : Form
    {
        private readonly ILogger<TimerTaskForm> _logger;          // 日志记录器
        private readonly IConfiguration _configuration;         // 配置信息
        private List<TaskItem> _tasks;
        private SQLiteHelper<TaskItem> _sqliteHelper;

        public TimerTaskForm(ILogger<TimerTaskForm> logger, IConfiguration configuration)
        {
            InitializeComponent();
            _logger = logger;
            _configuration = configuration;
            _sqliteHelper = new SQLiteHelper<TaskItem>();
        }


        private void TimerTaskForm_Load(object sender, EventArgs e)
        {
            lv_tasks.Columns.Add("Id", 50);
            lv_tasks.Columns.Add("任务名称", 100);
            lv_tasks.Columns.Add("任务时间", 150);
            lv_tasks.Columns.Add("任务重复", 80);
            lv_tasks.Columns.Add("任务状态", 80);
            lv_tasks.Columns.Add("任务简介", 200);
            _logger.LogInformation("定时任务窗口初始化");
            InitData();
        }

        private void btn_select_Click(object sender, EventArgs e)
        {
            InitData();
        }

        private void InitData()
        {
            _tasks = _sqliteHelper.QueryAll();
            lv_tasks.Items.Clear();
            foreach (var task in _tasks)
            {
                if (task.TaskName.Contains(txt_selectName.Text.Trim()))
                {
                    ListViewItem item = new ListViewItem();
                    item.Text = task.Id.ToString();
                    item.SubItems.Add(task.TaskName);
                    item.SubItems.Add(task.TaskTime);
                    item.SubItems.Add(task.TaskRept);
                    item.SubItems.Add(task.TaskStatus);
                    item.SubItems.Add(task.TaskInfo);
                    lv_tasks.Items.Add(item);
                }
            }
            _logger.LogInformation($"查询任务名称：{txt_selectName.Text.Trim()}");
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txt_name.Text))
            {
                MessageBox.Show("请输入任务名称！");
                return;
            }
            if (string.IsNullOrEmpty(dt_time.Text))
            {
                MessageBox.Show("请选择任务时间！");
                return;
            }
            _sqliteHelper.Insert(new TaskItem()
            {
                TaskName = txt_name.Text,
                TaskInfo = rtxt_info.Text,
                TaskRept = cb_rept.Text,
                TaskTime = dt_time.Text,
                TaskStatus = rb_run.Checked ? "运行" : "禁用"
            });
            _logger.LogInformation($"添加任务成功，任务名称：{txt_name.Text}");
            InitData();
        }

        private void lv_tasks_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lv_tasks.SelectedItems.Count > 0)
            {
                TaskItem task = _tasks.FirstOrDefault(x => x.Id == Convert.ToInt32(lv_tasks.SelectedItems[0].Text));
                txt_Id.Text = task.Id.ToString();
                txt_name.Text = task.TaskName;
                rtxt_info.Text = task.TaskInfo;
                cb_rept.Text = task.TaskRept;
                dt_time.Text = task.TaskTime;
                if (task.TaskStatus == "运行")
                {
                    rb_run.Checked = true;
                }
                else
                {
                    rb_off.Checked = true;
                }
                _logger.LogInformation($"选择任务成功，任务名称：{task.TaskName}");
            }
        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            if (lv_tasks.SelectedItems.Count > 0)
            {
                TaskItem task = _tasks.FirstOrDefault(x => x.Id == Convert.ToInt32(lv_tasks.SelectedItems[0].Text));
                _sqliteHelper.Delete(task.Id);
                _logger.LogInformation($"删除任务成功，任务名称：{task.TaskName}");
                InitData();
            }
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            TaskItem task = _tasks.FirstOrDefault(x => x.Id == Convert.ToInt32(txt_Id.Text));
            if (!string.IsNullOrEmpty(txt_Id.Text) && task != null)
            {
                task.TaskName = txt_name.Text;
                task.TaskInfo = rtxt_info.Text;
                task.TaskRept = cb_rept.Text;
                task.TaskTime = dt_time.Text;
                task.TaskStatus = rb_run.Checked ? "运行" : "禁用";
                _sqliteHelper.Update(task);
                _logger.LogInformation($"保存任务成功，任务名称：{task.TaskName}");
                InitData();
            }
        }

    }
}
